package com.sbwebsocket.core.service;


import com.sbwebsocket.core.model.bo.UnusualMessage;
import com.sbwebsocket.core.model.contants.SendStatus;

import java.util.List;


public abstract class AbstractMessageDeliveryListener implements MessageDeliveryListener {

    private UnusualMessageManager unusualMessageManager;
    private OfflineMessageManager offlineMessageManager;

    protected AbstractMessageDeliveryListener(UnusualMessageManager unusualMessageManager,
                                              OfflineMessageManager offlineMessageManager) {
        this.unusualMessageManager = unusualMessageManager;
        this.offlineMessageManager = offlineMessageManager;
    }

    @Override
    public void handle(SendStatus status, String clientIdentity, String msg) {
        switch (status){
            case OFF_LINE:
                offlineMessageManager.save(new UnusualMessage(clientIdentity, msg));
                break;
            case FAIL:
                //投递到中间件,延迟发送
                unusualMessageManager.save(new UnusualMessage(clientIdentity, msg));
                break;
        }
    }

    @Override
    public void handle(SendStatus status, List<String> clientIdentitys, String msg) {
        for (String clientIdentity : clientIdentitys) {
            handle(status,clientIdentity, msg);
        }
    }

}
